package com.flink.api.unit7;

import org.apache.flink.api.common.functions.Partitioner;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * shuffle
 *
 * @author caizhiyang
 * @since 2024-02-23
 */
public class custom_6 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        DataStreamSource<String> stream = env.socketTextStream("192.168.226.130", 7777);

        /**
         * 参数一: 自定义分区器
         * 参数二: 分区key
         */
        stream.partitionCustom(new MyPartitioner(),s -> s).print();

        env.execute();
    }

    public static class MyPartitioner implements Partitioner<String> {
        @Override
        public int partition(String s, int i) {
            //第一个参数为值
            //第二个参数为分区数量
            return Integer.parseInt(s) % i;
        }
    }
}
